<?php

class SF_Passport_Helper_Data extends Mage_Core_Helper_Abstract
{
	protected $_host;
    
	public function __construct()
	{
	    $this->setHost();
	}
	
	public function setHost($host = null)
	{
	    $defaultHost = 'http://127.0.0.1:8080/rumall/services/rumallInterService?wsdl';
	    Mage::log('护照信息的URL:'. $defaultHost);
	    if(!$host)
	    {
	        $this->_host = $defaultHost;
	        return;
	    }
	    $this->_host = $host;
	}
	
	public function save($customId, $fristName, $middleName, $lastName, $passportNo, $issueDate)
	{
	    /* $passprotData 表单提交过来的值 */
	    $xml = '';
	    $xml .= "<Request>";
	    $xml .= "<AccountId>{$customId}</AccountId>";
	    $xml .= "<PassportNo>{$passportNo}</PassportNo>";
	    $xml .= "<FirstName>{$fristName}</FirstName>";
	    $xml .= "<MiddleName>{$middleName}</MiddleName>";
	    $xml .= "<LastName>{$lastName}</LastName>";
	    $xml .= "<IssueDate>{$issueDate}</IssueDate>";
	    $xml .= "<IsAgree>1</IsAgree>";
	    $xml .= "</Request>";
	    
	    Mage::log('保存护照信息的xml:');
	    Mage::log($xml);
	
	    ini_set("soap.wsdl_cache_enabled", "0");
	    try
	    {
	        $soap   = new SoapClient($this->_host);
	        $result = $soap->setPassport($xml);
            $xmlobj = simplexml_load_string($result);
		    if($xmlobj->Head == 'OK')
		    {
		        Mage::log('保存护照信息的状态:'. $xmlobj->Head);
		        return true;
		    }
		    else 
		    {
		        Mage::log('保存护照信息的状态:'. $xmlobj->Head);
		        Mage::log('保存护照时报错:DEBUG_ERROR');
		        return false;
		    }
	    }
	    catch (Exception $e)
	    {
	        Mage::log('保存护照时报错:DEBUG_ERROR');
	        Mage::log($e->getMessage());
	        return false;
	    }
	
	}
	
	public function getPassport($customId)
	{
	    try
	    {
	        $soap   = new SoapClient($this->_host);
	        $result = $soap->getPassport($customId);
	        
	        Mage::log('获取护照信息时的customId:'. $customId);
	        Mage::log('获取护照信息返回的内容:');
	        Mage::log($result);
	        $xmlobj = simplexml_load_string($result);
	        if($xmlobj->Head == 'OK')
	        {
	            $passportArr = json_decode(json_encode($xmlobj),true);
	            $passportArr = $passportArr['Body'];
	            
	            Mage::log('获取护照信息返回的内容变成数组:');
	            Mage::log($passportArr);
	            return $passportArr;
	        }
	        
	        if($xmlobj->Head == 'ERROR')
	        {
	        	Mage::log('保存护照时报错:DEBUG_ERROR');
	        	Mage::log('保存护照信息的状态:'. $xmlobj->Head);
	            return false;
	        }
	    }
	    catch (Exception $e)
	    {
	    
	        Mage::log('获取护照时报错:DEBUG_ERROR');
	        Mage::log($e->getMessage());
	        return false;
	    }
	}

	public function checkIsNeedPassport()
	{
		$quote = Mage::getSingleton('checkout/session')->getQuote();
		$cartItems = $quote->getAllVisibleItems();
		foreach ($cartItems as $item) 
		{
		    $productId = $item->getProductId();
		    $product = Mage::getModel('catalog/product')->load($productId);
		    if($product->getCommonShippingType() == '14') /*只有专递才需要护照信息*/
		    {
		    	return true;
		    }
		}
		return false;
	}
}
